<?php 
include("../include/session.php"); 
include('../geocoder.php');

class ModProcess
{
	function ModProcess()
	{
		global $session, $config;
		/* Make sure administrator is accessing page */
		if(!$session->isModerator())
		{
			header("Location: ".$config['WEB_ROOT'].$config['home_page']);
			return;
		}
		/* Moderator submitted organization update form */
		if(isset($_POST['moderator']) && isset($_POST['id']) )
		{
			if($_POST['button'] == "Confirm Changes")
			{
				$this->modConfirm();
			}
			else if($_POST['button'] == "Delete Request")
			{
				$this->modDelete();
			}
			else if($_POST['button'] == "Save Request")
			{
				$this->modSave();
			}
			else if($_POST['button'] == "Priority")
			{
				$this->modPriority();
			}
				
		}
		else
		{
			header("Location: moderator.php");
		}
	}
	
	function modConfirm()
	{
		global $session, $database, $form;
		$id = $_POST['id'];
		$sql = $database->connection->query("SELECT * FROM ".TBL_UPDATE_QUEUE." WHERE id='$id'");
		$result = $sql->fetch();
		
		$orgID = $result['orgID'];
		$orgName = $result['orgName'];
		$areaCode = $result['areaCode'];
		$prefix = $result['prefix'];
		$lineNumber = $result['lineNumber'];
		$faxAreaCode = $result['faxAreaCode'];
		$faxPrefix = $result['faxPrefix'];
		$faxLineNumber = $result['faxLineNumber'];
		$streetAddress = $result['streetAddress'];
		$streetAddress2 = $result['streetAddress2'];
		$city = $result['city'];
		$state = $result['state'];
		$zipCode = $result['zipCode'];
		$websiteAddress = $result['websiteAddress'];
		$email = $result['email'];
		$category = $result['category'];
		$active = $result['BusinessIsInactive'];
		
		// GEOCODE
		$address = $streetAddress." ".$streetAddress2." ".$state." ".$zipCode;
		$geocodeAddress = preg_replace('/\s+/', '+', $address);
		$geocode = geocode($geocodeAddress);
		$lat = $geocode["lat"];
		$lng = $geocode["lng"];
		
		$query = "UPDATE ".TBL_ORGANIZATIONS." SET orgName='$orgName', email='$email', areaCode='$areaCode', prefix='$prefix', lineNumber='$lineNumber', faxAreaCode='$faxAreaCode', faxPrefix='$faxPrefix', faxLineNumber='$faxLineNumber', streetAddress='$streetAddress', streetAddress2='$streetAddress2', city='$city', state='$state', zipCode='$zipCode', websiteAddress='$websiteAddress', lat='$lat', lng='$lng', category='$category', Status='$active' WHERE orgID='$orgID'";
		
		$stmt = $database->connection->prepare($query);
		$stmt->execute();
	
		// DELETE THE POST AFTER FINISH
		$sql = $database->connection->prepare("DELETE FROM ".TBL_UPDATE_QUEUE." WHERE id = '$id'");
		$sql->execute();
		
		header("Location: moderator.php");
	}
	
	function modDelete()
	{
		global $session, $database, $form;
		$id = $_POST['id'];
		
		$sql = $database->connection->prepare("DELETE FROM ".TBL_UPDATE_QUEUE." WHERE id = '$id'");
		$sql->execute();
		
		header("Location: moderator.php");
	}
	
	function modSave()
	{
		global $session, $database, $form;
		$id = $_POST['id'];
		
		$sql = $database->connection->prepare("UPDATE ".TBL_UPDATE_QUEUE." SET modAction='saved' WHERE id = '$id'");
		$sql->execute();
		
		header("Location: moderator.php");
	}
}

$modProcess = new ModProcess;
?>